﻿
layui.config({
    base: '/general/testzs/public/static/layui2.6.8/lay/modules/' //假设这是test.js所在的目录   可以把你需要扩展的js插件都放在一个文件夹内
}).extend({ //设定组件别名
    printer: 'printer'
});

layui.use(['form','printer','element','table','layer','upload'], function(){
    //直接渲染表格
    var form = layui.form;
    var table = layui.table;
    var printer = layui.printer;
    var element = layui.element;
    var upload = layui.upload;
    var $ = layui.$;
    //我的印刷
    table.render({
        elem: '#printingTable'
        ,headers: {
            Accept: "application/json; charset=utf-8",
        }
        ,height: 700
        ,url: '/general/testzs/public/printing/index/MyPrinting'
        ,cols: [[ //表头
            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
            { title: 'printing_id', hide:true, }
            ,{field: 'printing_number', title: '申请单号'}
            ,{field: 'customer_name', title: '客户名称'}
            ,{field: 'order_number', title: '订单号'}
            ,{field: 'user_name', title: '制单人'}
            ,{field: 'create_time', title: '发单时间'}
            ,{field: 'tips', title: '当前状态'}
            ,{field:'right', title: '操作',toolbar:"#PrintingTool"}
            ,{ title: '采购状态',field:'purchase_status',  hide:true, }
            ,{ title: '总经办状态',field:'general_status', hide:true, }
            ,{ title: '质检部状态',field:'inspector_status', hide:true, }
            ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
            ,{ title: '总经办审批时间',field:'general_time', hide:true, }
            ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
            ,{ title: '采购部内容',field:'purchase_content', hide:true, }
            ,{ title: '质检部内容',field:'inspector_content', hide:true, }
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 200 //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'count' //规定数据总数的字段名称，默认：count
            , dataName: 'data' //规定数据列表的字段名称，默认：data
        }
        ,page: true
        ,text: { none: '暂无印刷记录' }
    });

    table.on('tool(printingTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            //打开详情
            layer.open({
                type: 1,
                area: ['1000px','800px'],
                offset: '100px',
                title: data.user_name+'的印刷详情',
                content: $('#PrintingDetailHtml').html(), // 注意这里有.html()
                success: function(layero, dIndex) {
                    //查询类别 渲染上去
                    //直接从 改列那值渲染,列或者 请求都可以
                    $('#customer_name_view').val(data.customer_name); //data.printing_number
                    $('#order_number_view').val(data.order_number); //data.printing_number
                    $('#remarks_view').val(data.remarks);
                    $('#purchase_content_view').val(data.purchase_content); //data.printing_number
                    $('#inspector_content_view').val(data.inspector_content);

                    $("#printing_price_view").val(data.printing_price);
                    $("#currency_view").val(data.currency);
                    //币种做判断
                    var array_currency_view = ['人民币','美金','欧元'];
                    var html_currency = '';
                    for (var i = 0;i < array_currency_view.length;i++){
                        if (data.currency == array_currency_view[i]){
                            html_currency += '<option selected value='+  array_currency_view[i] +'>'+ array_currency_view[i] +'</option>';
                        }else{
                            html_currency += '<option value='+  array_currency_view[i] + '>'+ array_currency_view[i] +'</option>';
                        }
                    }
                    $('#currency_view').append(html_currency);

                    $("input[name='is_collect_cost_view'][value='"+ data.is_collect_cost +"']").prop("checked", "checked");
                    //审批下列
                    form.render();
                    $('#user_name').val(data.user_name); //data.printing_number
                    $('#create_time').val(data.create_time); //data.printing_number
                    if (data.purchase_status === 2){
                        $('#purchase_tips').val('未审批');
                    }else if (data.purchase_status === 1){
                        $('#purchase_tips').val('通过');
                        $('#purchase_time').val(data.purchase_time);
                    }else if (data.purchase_status === 3){
                        $('#purchase_tips').val('拒绝');
                        $('#purchase_time').val(data.purchase_time);
                    }
                    if (data.general_status === 2){
                        $('#general_tips').val('未审批');
                    }else if (data.general_status === 1){
                        $('#general_tips').val('通过');
                        $('#general_time').val(data.general_time);
                    }else if (data.general_status === 3){
                        $('#general_tips').val('拒绝');
                        $('#general_time').val(data.general_time);
                    }
                    if (data.inspector_status === 2){
                        $('#inspector_tips').val('未审批');
                    }else if (data.inspector_status === 1){
                        $('#inspector_tips').val('通过');
                        $('#inspector_time').val(data.inspector_time);
                    }else if (data.inspector_status === 3){
                        $('#inspector_tips').val('拒绝');
                        $('#inspector_time').val(data.inspector_time);
                    }
                    //是否驳回
                    if (data.status === 5){
                        $('#reject_status').show();
                        $('#reject_content_view').val(data.reject_content);
                        //打开input框 ，可重新提交
                        // $('#customer_name_view').attr('disabled',false);
                        // $('#order_number_view').attr('disabled',false);
                        $('#remarks_view').attr('disabled',false);

                        $('[name="is_collect_cost_view"]').attr("disabled",false); //
                        $('#currency_view').attr("disabled",false)
                        $("#printing_price_view").attr("disabled",false);

                        $('#AgainButton').show();
                        // $('#customer_name_view').css('border','1px solid');
                        // $('#order_number_view').css('border','1px solid');
                        $('#remarks_view').css('border','1px solid');
                        $("#currency_view").css('border','1px solid');
                        $('#printing_price_view').css('border','1px solid');
                        form.render();

                        table.render({
                            elem: '#articlesDetail'
                            ,height: 312
                            ,where:{
                                printing_id:data.printing_id
                            }
                            ,headers: {
                                Accept: "application/json; charset=utf-8",
                            }
                            ,url: '/general/testzs/public/printing/index/PrintingArticles' //正常表
                            ,cols: [[ //表头
                                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                                { title: 'articles_id', hide:true, }
                                ,{field: 'value', title: '类别名称', width:100}
                                ,{field: 'name', title: '名称', width:100}
                                ,{field: 'articles_content', title: '内容', width:150}
                                ,{field: 'special', title: '特殊要求及备注', width:150}
                                ,{field: 'articles_edition', title: '印刷版号', width:120}
                                ,{field: 'user_file', title: '附件', width:150}
                                ,{field: '', title: '操作',toolbar:"#ArticlesEdit"}
                            ]]
                            ,response: {
                                statusName: 'code' //规定数据状态的字段名称，默认：code
                                , statusCode: 200 //规定成功的状态码，默认：0
                                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                                , countName: 'count' //规定数据总数的字段名称，默认：count
                                , dataName: 'data' //规定数据列表的字段名称，默认：data
                            }
                            ,page: true
                        });
                        //重新提交
                        $('#AgainCommit').click(function(){

                            layer.confirm('您确定已经修改完成，要重新提交吗？', function(index){

                                var customer_name_view =  $('#customer_name_view').val();
                                if (customer_name_view === ''){
                                    layer.alert('客户名称不能为空');
                                    return false;
                                }
                                var order_number_view =  $('#order_number_view').val();
                                if (order_number_view === ''){
                                    layer.alert('订单号不能为空');
                                    return false;
                                }
                                var remarks_view =  $('#remarks_view').val();
                                if (remarks_view === ''){
                                    layer.alert('确认内容（备注）不能为空');
                                    return false;
                                }

                                var currency_view =  $('#currency_view').val(); //币种

                                var is_collect_cost_view = $('input:radio[name="is_collect_cost_view"]:checked').val();

                                var printing_price_view =  $('#printing_price_view').val();
                                if (printing_price_view === ''){
                                    layer.alert('版费金额不能为空');
                                    return false;
                                }

                                var loading = layer.load(0, {
                                    shade: false,
                                    time: 2*1000
                                });
                                $.ajax({
                                    type: "POST",
                                    url: '/general/testzs/public/printing/Index/AgainCommit',
                                    data: {
                                        'printing_id' :data.printing_id,
                                        'customer_name':customer_name_view,
                                        'order_number':order_number_view,
                                        'remarks':remarks_view,
                                        'currency':currency_view,
                                        'is_collect_cost':is_collect_cost_view,
                                        'printing_price':printing_price_view
                                    },
                                    dataType:"json",
                                    success: function(data){
                                        layer.close(loading);
                                        if (data.code === 200){
                                            layer.alert(data.msg);
                                            layer.close(dIndex);
                                            table.reload('printingTable',{});
                                        }else{
                                            layer.alert(data.msg);
                                        }
                                    }
                                })
                                layer.close(index);
                            });
                        })
                    }else{

                        table.render({
                            elem: '#articlesDetail'
                            ,height: 312
                            ,where:{
                                printing_id:data.printing_id
                            }
                            ,headers: {
                                Accept: "application/json; charset=utf-8",
                            }
                            ,url: '/general/testzs/public/printing/index/PrintingArticles' //正常表
                            ,cols: [[ //表头

                                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                                { title: 'articles_id', hide:true, }
                                ,{field: 'value', title: '类别名称', width:150}
                                ,{field: 'name', title: '名称', width:150}
                                ,{field: 'articles_content', title: '内容', width:200}
                                ,{field: 'special', title: '特殊要求及备注', width:150}
                                ,{field: 'articles_edition', title: '印刷版号', width:120}
                                ,{field: 'user_file', title: '附件', width:150}

                            ]]
                            ,response: {
                                statusName: 'code' //规定数据状态的字段名称，默认：code
                                , statusCode: 200 //规定成功的状态码，默认：0
                                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                                , countName: 'count' //规定数据总数的字段名称，默认：count
                                , dataName: 'data' //规定数据列表的字段名称，默认：data
                            }
                        });
                    }

                    //已审批内展示文件
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/index/PrintingFileAll',
                        data: {
                            'printing_id':data.printing_id
                        },
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                $('#file_content').html(data.data);
                            }else{

                            }
                        }
                    })

                    $("#print").click(function () {

                        $(".layui-table-page").addClass("hide-print");
                        $(".layui-layer-title").addClass("hide-print");
                        $(".layui-layer-setwin").addClass("hide-print");
                        var xx = $(".layui-layer-page").html();
                        //这个是操作 没打印前的
                        $(".layui-layer-page").css("top", "0px");
                        $(".layui-layer-page").css("left", "0px");

                        setTimeout(function () {
                            //做操作 会弹出两个页面 太窄了吗
                            printer.print(); //做操作
                        }, 1000); //触发事件
                    })

                },end : function() {
                    //回调后结束操作

                }

            });
        }
    });
    //添加类别
    $("#add_printing").click(function(){
        layer.open({
            type: 1,
            area: ['1000px','800px'],
            offset: '100px',
            title: '添加印刷单',
            content: $('#addPrintingHtml').html(), // 注意这里有.html()
            success: function(layero, dIndex) {
                //查询类别 渲染上去
                //获取订单
                $.ajax({
                    type: "POST",
                    url: '/general/testzs/public/printing/index/getMyUserOrder',
                    data: {},
                    traditional: true,
                    dataType:"json",
                    success: function(data){
                        if (data.code === 200){
                            $.each(data.data, function(index, item) {
                                $('#customer_name').val(data.data[0].cCusName);
                                $('#order_number').append('<option value=' + item.order + '>' + item.order +'</option>');
                            });
                            form.render();
                        }
                    }
                })
                form.on('select(order_number)', function(data_ss){
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/index/getMyUserOrder',
                        data: {},
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                $.each(data.data, function(index, item) {
                                    if (data_ss.value == item.order){
                                        $('#customer_name').val(item.cCusName);
                                        $('#cCusCode').val(item.cCusCode);
                                    }
                                });
                            }
                        }
                    })
                });
                //暂存表
                table.render({
                    elem: '#articlesBat'
                    ,height: 312
                    ,headers: {
                        Accept: "application/json; charset=utf-8",
                    }
                    ,url: '/general/testzs/public/printing/index/ArticlesBatList' //暂存表
                    ,cols: [[ //表头
                        { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                        { title: 'articlesbat_id', hide:true, }
                        ,{field: 'value', title: '类别名称', width:100}
                        ,{field: 'name', title: '名称', width:100}
                        ,{field: 'articles_content', title: '内容', width:150}
                        ,{field: 'special', title: '特殊要求及备注', width:150}
                        ,{field: '', title: '印刷版号', width:120}
                        ,{field: 'user_file', title: '附件', width:150}
                        ,{field:'right', title: '操作',toolbar:"#barArticlesBat"}
                    ]]
                    ,response: {
                        statusName: 'code' //规定数据状态的字段名称，默认：code
                        , statusCode: 200 //规定成功的状态码，默认：0
                        , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                        , countName: 'count' //规定数据总数的字段名称，默认：count
                        , dataName: 'data' //规定数据列表的字段名称，默认：data
                    }
                   // ,page: true
                    ,text: { none: '暂无添加，您需要先添加类别商品才可以最后提交' }
                });
                //获取类别
                $.ajax({
                    type: "POST",
                    url: '/general/testzs/public/printing/index/getCategoryType',
                    data: {},
                    traditional: true,
                    dataType:"json",
                    success: function(data){
                        if (data.code === 200){
                            $.each(data.data, function(index, item) {
                                $('#value_bash').append('<option value=' + item.value + '>' + item.value +'</option>');
                            });
                            form.render('select');
                        }
                    }
                })
                // 添加暂存
                upload_to();
                form.on('submit(articlesBatAdd)', function(data) {
                    $('#articlesBatButton').attr('disabled',false)
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/Index/AddArticlesBat',
                        data: data.field,
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                //重新刷新表格\
                                $('#articles_content').val('');
                                table.reload('articlesBat', {})
                            }else{
                                layer.alert(data.msg);
                            }
                            $('#articlesBatButton').removeAttr('disabled')
                        }
                    })
                    return false;
                });
                // 最后表单提交事件
                form.on('submit(PrintingAdd)', function(data) {

                    var customer_name =  $('#customer_name').val();
                    if (customer_name === ''){
                        layer.alert('客户名称不能为空');
                        return false;
                    }
                    var order_number =  $('#order_number').val();
                    if (order_number === ''){
                        layer.alert('订单号不能为空');
                        return false;
                    }
                    var remarks =  $('#remarks').val();
                    if (remarks === ''){
                        layer.alert('确认内容（备注）不能为空');
                        return false;
                    }
                    var cCusCode = $('#cCusCode').val();

                    var currency =  $('#currency').val(); //币种

                    var is_collect_cost = $('input:radio[name="is_collect_cost"]:checked').val();

                    var printing_price =  $('#printing_price').val();
                    if (printing_price === ''){
                        layer.alert('版费金额不能为空');
                        return false;
                    }
                    var loading = layer.load(0, {
                        shade: false,
                        time: 2*1000
                    });
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/Index/CommitPrinting',
                        data: {
                            'customer_name':customer_name,
                            'order_number':order_number,
                            'remarks':remarks,
                            'cCusCode':cCusCode,
                            'currency':currency,
                            'is_collect_cost':is_collect_cost,
                            'printing_price':printing_price
                        },
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            layer.close(loading);
                            if (data.code === 200){
                                layer.alert(data.msg);
                                table.reload('articlesBat', {})
                                table.reload('printingTable',{});
                            }else{
                                layer.alert(data.msg);
                            }

                        }
                    })
                    return false;
                });
            },end : function() {
                //回调后结束操作
            }


        });
    });

    //删除暂存操作
    table.on('tool(articlesBat)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('要删除暂存吗', function(index){
                $.ajax({
                    type: "POST",
                    url: '/general/testzs/public/printing/Index/DelArticlesBat',
                    data: {
                        'articlesbat_id':data.articlesbat_id
                    },
                    dataType:"json",
                    success: function(data){
                        if (data.code === 200){
                            layer.alert(data.msg);
                            obj.del();
                        }else{
                            layer.alert(data.msg);
                        }
                    }
                })
                layer.close(index);
            });
        }
    });

    //编辑重新提交操作
    table.on('tool(articlesDetail)', function(obj){
        var superior_data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
                type: 1,
                area: ['500px','400px'],
                offset: '100px',
                title: '编辑该条分类内容',
                content: $('#EditArticlesHtml').html(), // 注意这里有.html()
                success: function(layero,index) {
                    //请求默认选中下拉框
                    $('#articles_content_view').val(superior_data.articles_content);
                    $('#special_view').val(superior_data.special);
                    $('#name_view').val(superior_data.name);
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/index/LabelValueList',
                        data: {},
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                $.each(data.data, function(index, item) {
                                    if (superior_data.value == item.value){
                                        $('#value_view').append('<option selected value=' + item.value + '>' + item.value +'</option>');
                                    }else{
                                        $('#value_view').append('<option value=' + item.value + '>' + item.value +'</option>');
                                    }
                                });
                                form.render('select');
                            }
                        }
                    })
                    form.on('submit(EditArticlesButton)', function(data) {

                        $.ajax({
                            type: "POST",
                            url: '/general/testzs/public/printing/Index/EditArticlesType?articles_id='+superior_data.articles_id,
                            data: data.field,
                            traditional: true,
                            dataType:"json",
                            success: function(data_end){
                                if (data_end.code === 200){
                                    //重新刷新表格\
                                    layer.msg(data_end.msg, {
                                        icon: 1
                                    });
                                    setTimeout(function () {
                                        layer.close(index);
                                        table.reload('articlesDetail', {})
                                    }, 500);
                                }else{
                                    layer.msg(data_end.msg, {
                                        icon: 2
                                    });
                                }
                            }
                        })

                        return false;
                    });

                },end : function() {
                    //回调后结束操作
                }

            });

        }
    });

    /* 表格搜索 */
    form.on('submit(SearchForm)', function (data) {
        // 渲染项目表格
        table.render({
            elem: '#printingTable'
            ,headers: {
                Accept: "application/json; charset=utf-8",
            }
            ,where:{
                'printing_number' :data.field.printing_number,
                'customer_name':data.field.customer_name,
                'order_number' :data.field.order_number,
                'status':data.field.status
            }
            ,height: 700
            ,url: '/general/testzs/public/printing/index/MyPrinting'
            ,cols: [[ //表头
                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                { title: 'printing_id', hide:true, }
                ,{field: 'printing_number', title: '申请单号'}
                ,{field: 'customer_name', title: '客户名称'}
                ,{field: 'order_number', title: '订单号'}
                ,{field: 'user_name', title: '制单人'}
                ,{field: 'create_time', title: '发单时间'}
                ,{field: 'tips', title: '当前状态'}
                ,{field:'right', title: '操作',toolbar:"#PrintingTool"}
                ,{ title: '采购状态',field:'purchase_status',  hide:true, }
                ,{ title: '总经办状态',field:'general_status', hide:true, }
                ,{ title: '质检部状态',field:'inspector_status', hide:true, }
                ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
                ,{ title: '总经办审批时间',field:'general_time', hide:true, }
                ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
                ,{ title: '采购部内容',field:'purchase_content', hide:true, }
                ,{ title: '质检部内容',field:'inspector_content', hide:true, }
            ]]
            ,response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            ,page: true
            ,text: { none: '暂无印刷记录' }
        });
        return false;
    });

    function upload_to(){
        //指定允许上传的文件类型
        upload.render({
            elem: '#test1'
            ,url: '/general/testzs/public/printing/index/toUpload' //此处配置你自己的上传接口即可
            ,accept: 'file' //普通文件
            ,done: function(res){
                if (res.code === 200){
                    layer.msg('上传成功');
                    var substr = res.name.substring(0, 3);
                    $('#file_name').text('ok!'+ substr + '..');
                    $('#file_src').val(res.src);
                    $('#user_file_name').val(res.name);
                }else{
                    layer.msg('上传失败');
                }
            }
        });
    }

});

